/***************************************************************************
 *   Copyright (C) 2008 by Marco Gulino   *
 *   marco.gulino@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA            *
 ***************************************************************************/
#include "filesscanner.h"
#include <QDebug>
#include "collectionutils.h"

FilesScanner::FilesScanner(QObject *parent)
    : QObject(parent)
{
    indexedFilesCount=0;
}

FilesScanner::~FilesScanner()
{
}

FilesScanner& FilesScanner::addScanDirectory(QDirInterfacePointer directory)
{
    directories << directory;
    connect(directory.data(), SIGNAL(newFileIndexed()),this,SLOT(newFileIndexed()));
    return *this;
}


qint64 FilesScanner::scanForFiles()
{
    indexedFilesCount = 0;
    m_foundFiles.clear();
    foreach(QDirInterfacePointer dir, directories)
    {
//        dir->stop_flag = false;
        m_foundFiles += *dir->findAllFiles();
    }
    emit indexedFilesCountChanged();
    CollectionUtils::deleteUniqueValues(&m_foundFiles);
    return m_foundFiles.count();
}
qint64 FilesScanner::scanForEmptyDirs()
{
    emptyDirs.clear();
    indexedFilesCount = 0;
    foreach(QDirInterfacePointer dir, directories)
    {
        dir->stop_flag = false;
        emptyDirs += *dir->findEmptyDirs();
    }
    emit indexedFilesCountChanged();
    return emptyDirs.count();
}
QList<QDirSharedPointer> FilesScanner::getEmptyDirs()
{
    return emptyDirs;
}
void FilesScanner::newFileIndexed()
{
    indexedFilesCount++;

    if (0 == (indexedFilesCount % INDEXED_FILES_COUNTER_SIZE))
        emit indexedFilesCountChanged();
}

void FilesScanner::stop()
{
    foreach(QDirInterfacePointer dir, directories)
    {
        dir->stop();
    }
}
